package com.cc.openeye.dynamic.kuikly.recommend.items.square

import com.cc.openeye.dynamic.recommend.entity.dynamicResponse.ItemX
import com.tencent.kuikly.core.base.Color
import com.tencent.kuikly.core.base.ComposeView
import com.tencent.kuikly.core.base.ComposeAttr
import com.tencent.kuikly.core.base.ComposeEvent
import com.tencent.kuikly.core.base.ViewBuilder
import com.tencent.kuikly.core.base.ViewContainer
import com.tencent.kuikly.core.directives.vfor
import com.tencent.kuikly.core.reactive.handler.observable
import com.tencent.kuikly.core.views.WaterfallList

internal class InternalSquareItemView : ComposeView<SquareItemViewAttr, SquareItemViewEvent>() {

    private var height by observable(0f)

    override fun createEvent(): SquareItemViewEvent {
        return SquareItemViewEvent()
    }

    override fun createAttr(): SquareItemViewAttr {
        return SquareItemViewAttr()
    }


    override fun body(): ViewBuilder {
        val ctx = this
        val pageWith = ctx.attr.flexNode?.styleWidth ?: 0f
        return {

            attr {
                width(pageWith)
                height(ctx.height)
            }

            WaterfallList {
                attr {
                    columnCount(2)
                    lineSpacing(10f)
                    itemSpacing(10f)
                    listWidth(pageWith)
                    height(ctx.height)
                }


                event {
                    if (ctx.height == 0f) {
                        contentSizeChanged { _, height ->
                            ctx.height = height
                        }
                    }
                }

                ctx.attr.itemList?.forEach {
                    SquareItemContentView {
                        attr {
                            data = it
                        }
                    }
                }
            }
        }
    }
}


internal class SquareItemViewAttr : ComposeAttr() {
    var itemList: List<ItemX>? = null

}

internal class SquareItemViewEvent : ComposeEvent() {

}

internal fun ViewContainer<*, *>.SquareItemView(init: InternalSquareItemView.() -> Unit) {
    addChild(InternalSquareItemView(), init)
}